#include "threadpool.h"


typedef struct train{
    int length;
    char data[1000];
}train_t;

int transFile(int netfd){
    train_t train;

    char filename[]="file1";
    train.length=5;
    memcpy(train.data,filename,train.length);
    send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    send(netfd,train.data,train.length,MSG_NOSIGNAL);

    struct  stat statbuf;
    bzero(&statbuf,sizeof(statbuf));
    if(stat(filename,&statbuf) == -1) {
        perror("stat");
        return -1;
    }
    off_t filesize=statbuf.st_size;
    printf("filename=%s , filesize=%ld\n",filename,filesize);
    train.length=sizeof(filesize);
    memcpy(&train.data,&filesize,train.length);
    send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    send(netfd,train.data,train.length,MSG_NOSIGNAL);
    
    int fd=open(filename,O_RDWR);
    if(fd == -1) {
        perror("open");
        return -1;
    }
    char *p=(char*) mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if(p == MAP_FAILED) {
        perror("mmap");
        close(fd);
        return -1;
    }
    send(netfd,p,filesize,MSG_NOSIGNAL);
    munmap(p,filesize);
    

    close(fd);
    return 0;
}